let cheerio = require('cheerio')
let http = require('http')
let iconv = require('iconv-lite')

let url = 'http://www.dytt8.net/html/gndy/dyzz/list_23_'
var titles = []

// 爬取
getTitles(url, 2, 4, titles)

/**
 * param {string} url
 * param {int} index that index > 0
 * param {int} n that the number of pages
 * param {string[]} titles 
 */
function getTitles(url, index, n, titles) {


  console.log(`正在爬取第${index}页的内容：`)
  console.log(url + index + '.html')

  http.get(url + index + '.html', (sres) => {

    let chunks = []
    sres.on('data', (chunk) => {
      chunks.push(chunk)
    })

    sres.on('end', () => {

      let html = iconv.decode(Buffer.concat(chunks), 'gb2312')
      let $ = cheerio.load(html, {
        decodeEntities: false
      })

      $('.co_content2 ul a').each((id, element) => {
        if (id > 3) {
          let $element = $(element)
          titles.push({
            title: $element.text()
          })
        }

      })
      if (n > 0) {
        getTitles(url, ++index, --n, titles)
      }else{
        console.log(titles);
        console.log("titles 获取完毕！");
      }

    })

  })

}
